package cl.utalca.robot.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cl.utalca.robot.dto.LanguageDTO;
import cl.utalca.robot.dto.TestDTO;
import cl.utalca.robot.services.LanguageService;
import cl.utalca.robot.services.RobotService;

/**
 * Servlet implementation class LanguageServlet
 */
@WebServlet("/LanguageController")
public class LanguageController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private LanguageService languageService;
	private static String ADD_OR_EDIT = "jsp/languageForm.jsp";
	private static String LIST = "jsp/administrator.jsp";
	private static String ERROR = "jsp/error.jsp";
    /**
     * Default constructor. 
     */
    public LanguageController() {
       super();       
       languageService = new LanguageService();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String forward="";
        String action = request.getParameter("action");

        if (action.equalsIgnoreCase("delete")){
            int languageId = Integer.parseInt(request.getParameter("Id"));
            Boolean success = languageService.deleteLanguage(languageId);
            forward = success ? LIST : ERROR;
            request.setAttribute("languageList", languageService.getLanguages());    
        } else if (action.equalsIgnoreCase("edit")){
            forward = ADD_OR_EDIT;
            int languageId = Integer.parseInt(request.getParameter("Id"));
            LanguageDTO language = languageService.getLanguage(languageId);
            request.setAttribute("language", language);            
        } else if (action.equalsIgnoreCase("list")){
            forward = LIST;
            request.setAttribute("languageList", languageService.getLanguages());
        } else {
            forward = ADD_OR_EDIT;
        }
        request.setAttribute("action", action);
        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String forward="";
		
		LanguageDTO language = new LanguageDTO();
		language.setLanguageName(request.getParameter("languageName"));
		language.setCompileCommand(request.getParameter("compileCommand"));
		language.setExecuteCommand(request.getParameter("executeCommand"));
		
		Boolean success = false;
		
		if(request.getParameter("id") == null || request.getParameter("id").isEmpty())
		{
			success = languageService.saveLanguage(language);
		}
		else{
			
			language.setId(Integer.parseInt(request.getParameter("id")));
			success = languageService.updateLanguage(language);
		}
       
        forward = success ? LIST : ADD_OR_EDIT;
        RequestDispatcher view = request.getRequestDispatcher(forward);
        request.setAttribute("languageList", languageService.getLanguages());
        view.forward(request, response);
	}

}
